#########################################################################################
####################################  FIGURE A5   #######################################
######  Tracing the source of disempowerment: Fraction of total votes for men  ##########
#################  and women, by source and across conditions  ##########################
#########################################################################################

rm(list=ls())

library(dplyr)
require(ggplot2)
require(ggpubr)
require(gridExtra)

colorW<-"#8856a7"
colorM<- "#9ebcda"

# Import Study 1 analysis produced in STATA do file
#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_a5")

#########################################################################################


stataexport <- read.csv("figure_a5_input.csv", stringsAsFactors = FALSE)

stataexport <- stataexport %>%
  mutate(condition_maj_min = case_when(
    condition_maj_min=="Majority Men Condition" ~ "Majority Male",
    condition_maj_min=="Majority Women Condition" ~ "Majority Female",
    TRUE ~ "999"
  ))

stataexport$condition_maj_min <- factor(stataexport$condition_maj_min, levels = c("Majority Male", "Majority Female"))


p1df <- stataexport %>% filter(variable=="votedForWomanMInf")
segx <- c(.6, 1.6,1.05, 2.05)
segxend <- c(.95, 1.95,1.40,  2.40)
p1df$`Source of votes` <- p1df$sex

p1 <-
    ggplot(p1df, aes(x=condition_maj_min, y=X_val, colour = `Source of votes`)) +
    expand_limits(y=c(0,.7)) +
    geom_bar(position=position_dodge(), fill = c(colorM,  colorM,colorW, colorW), stat="identity", size=.3) +
    theme(legend.position = c(0.3, 0.85))  + scale_color_manual(values = c(colorM, colorW)) + scale_fill_manual(breaks=c("Men", "Women")) +
    guides(color = guide_legend(override.aes = list(fill = c(colorM, colorW)))) +
    geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, group=`Source of votes`), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
    geom_segment(aes(x=segx, xend = segxend, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
    xlab("All votes") +
    ylab("Share 'most influential' votes for women") +
    theme(text=element_text(size=12),
          plot.title = element_text(hjust=.5),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          axis.line = element_line(colour="black")) +
    theme(legend.title=element_text(size=12),
          legend.text=element_text(size=10))

p2df <- stataexport %>% filter(variable=="selfvote" & sex=="Women")
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)

p2 <- ggplot(p2df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,.7)) +
  geom_bar( position=position_dodge(), fill = c(colorW, colorW), stat="identity", colour="black", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci,  group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Women's votes for self") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")


p3df <- stataexport %>% filter(variable=="otherFvote" & sex == "Women")
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)


p3 <- ggplot(p3df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,.7)) +
  geom_bar( position=position_dodge(), fill = c(colorW, colorW), stat="identity", colour="black", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci,  group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Women's votes for other women") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")

stack <- grid.arrange(p1,p2,p3, nrow = 1)


########################################

p1df <- stataexport %>% filter(variable=="votedForManMInf")
segx <- c(.6, 1.6,1.05, 2.05)
segxend <- c(.95, 1.95,1.40,  2.40)

p1 <- ggplot(p1df, aes(x=condition_maj_min, y=X_val, colour = sex)) +
  expand_limits(y=c(0,1.5)) +
  geom_bar(position=position_dodge(), fill = c(colorM,  colorM,colorW, colorW), stat="identity",  size=.3) +
  theme(legend.position = c(0.3, 0.85))  + scale_color_manual(values = c(colorM, colorW)) + scale_fill_manual(breaks=c("Men", "Women")) +
  guides(color = guide_legend(override.aes = list(fill = c(colorM, colorW)))) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci, group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx, xend = segxend, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("All votes") +
  ylab("'most influential' votes per woman") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_text(size=12),
        legend.text=element_text(size=10),
        legend.position="none")

p2df <- stataexport %>% filter(variable=="selfvote" & sex=="Men")
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)

p2 <- ggplot(p2df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,.7)) +
  geom_bar( position=position_dodge(), fill = c(colorM,  colorM), stat="identity", colour="black", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci,  group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Men's votes for self") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")


p3df <- stataexport %>% filter(variable=="otherMvote" & sex == "Men")
segx2 <- c(.75, 1.75)
segxend2 <- c(1.25, 2.25)


p3 <- ggplot(p3df, aes(x=condition_maj_min, y=X_val)) +
  expand_limits(y=c(0,.7)) +
  geom_bar( position=position_dodge(), fill = c(colorM,  colorM), stat="identity", colour="black", size=.3) +
  geom_errorbar(aes(ymin=X_val-ci, ymax=X_val+ci,  group=sex), width=.2, stat="identity", position=position_dodge(.9), colour="black") +
  geom_segment(aes(x=segx2, xend = segxend2, y = X_exp, yend = X_exp), colour = "#999999", size=1, linetype="dashed") +
  xlab("Men's votes for other men") +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y=element_blank(),
        axis.line = element_line(colour="black")) +
  theme(legend.title=element_blank(), legend.position = "none")

stack2 <- grid.arrange(p1,p2,p3, nrow = 1)

finalgraph <- grid.arrange(stack, stack2, nrow=2)
ggsave(finalgraph, file = 'figure_a5.pdf', width = 14, height = 10, units = "in")
